package rentadomus

import org.springframework.dao.DataIntegrityViolationException
class HouseController {

    //def index() { }
    def scaffold = House
    def save() {
        def houseInstance = new House(params)
        def dwellingInstance = new Dwelling(params)
        def placeInstance = new Place(params)
        placeInstance.idPerson = session.person
        houseInstance.idDwelling = dwellingInstance
        dwellingInstance.idPlace= placeInstance
        if (placeInstance.save(flush: true)) {
            if(dwellingInstance.save(flush: true)){
                if (!houseInstance.save(flush: true)) {
                    render(view: "create", model: [houseInstance: houseInstance])
                    return
                }
            }
        }
        redirect(controller:"uploadImage", action: "main", id: placeInstance.id)
    }
    
    def delete(Long id) {
        def warehouseInstance = House.get(id)
        
        if (!warehouseInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'warehouse.label', default: 'Warehouse'), id])
            redirect(action: "list")
            return
        }
        def hoesInstance = Dwelling.get(warehouseInstance.idDwelling.id)
        def placeInstance = Place.get(hoesInstance.idPlace.id)
        
        try {
            warehouseInstance.delete(flush: true)
            hoesInstance.delete(flush: true)
            placeInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'warehouse.label', default: 'Warehouse'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'warehouse.label', default: 'Warehouse'), id])
            redirect(action: "show", id: id)
        }
    }    
    
    def filterSearch(Integer max){
        def option = params.option
        if(option=="1"){
            def filter = params.filter
            def fVal=params.value
            def tVal= params.value2
            params.max = Math.min(max ?: 10, 100)
            [houseInstanceList: House.list(params), houseInstanceTotal: House.count(),valueP: fVal, 
                valueP2: tVal, placeInstanceList:Place.list(params), filterB: filter]
        }
        else{
            def value = params.value
            def filter = params.filter
            params.max = Math.min(max ?: 10, 100)
            [houseInstanceList: House.list(params), houseInstanceTotal: House.count(), valueP: value, 
                placeInstanceList:Place.list(params), filterB: filter]
        }
    }
    
    def show(Long id) {
        def houseInstance = House.get(id)
        if (!houseInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'apartament.label', default: 'Apartament'), id])
            redirect(action: "list")
            return
        }
        def dwellingInstance=houseInstance.idDwelling
        def placeInstance = Place.get(dwellingInstance.idPlace.id)
        def visible=session.person && placeInstance.getIdPerson().getId()==session.person.id
        [houseInstance: houseInstance,placeInstance: placeInstance,dwellingInstance:dwellingInstance,phones:Phone.findAllByIdPerson(placeInstance.getIdPerson()),visible:visible]
    }
     

}
